# -*- coding:utf-8 -*-
import json
import datetime
import random
import requests
from requests_toolbelt import MultipartEncoder

'''
sqlite_server python3接口demo
'''

domain = 'http://127.0.0.1:13306'
session = 'Km5Uft8YQ__w_KeO'

#添加用户
def add_user(username):
	api = domain + '/api/add_user'
	request_data = {'username':username}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#获得用户列表
def get_users():
	api = domain + '/api/get_users'
	data = {'session':session}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#删除用户
def del_user(uid):
	api = domain + '/api/del_user'
	request_data = {'uid':uid}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#修改密码
def change_pwd(password):
	api = domain + '/api/change_pwd'
	request_data = {'password':password}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#重置session
def reset_session():
	api = domain + '/api/reset_session'
	data = {'session':session}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#开关权限
def auth_switch(uid, type, flag):
	api = domain + '/api/auth_switch'
	request_data = {'uid':uid,'type':type,'flag':flag}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#获得权限状态
def get_auth(uid):
	api = domain + '/api/get_auth'
	request_data = {'uid':uid}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#创建数据库
def create_db(dbname):
	api = domain + '/api/create_db'
	request_data = {'dbname':dbname}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#删除数据库
def drop_db(dbname):
	api = domain + '/api/drop_db'
	request_data = {'dbname':dbname}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#获得数据库列表
def get_dbs():
	api = domain + '/api/get_dbs'
	data = {'session':session}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#创建数据表
def create_table(dbname,tablename,columns):
	api = domain + '/api/create_table'
	request_data = {'dbname':dbname,'tablename':tablename,'columns':columns}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#删除数据表
def drop_table(dbname,tablename):
	api = domain + '/api/drop_table'
	request_data = {'dbname':dbname,'tablename':tablename}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#获得数据表列表
def get_tables(dbname):
	api = domain + '/api/get_tables'
	request_data = {'dbname':dbname}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#插入数据
def insert(dbname,tablename,data):
	api = domain + '/api/insert'
	request_data = {'dbname':dbname,'tablename':tablename,'data':data}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#插入数据
def insert2(dbname,tablename,data):
	api = domain + '/api/insert2'
	request_data = {'dbname':dbname,'tablename':tablename,'data':data}
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#删除数据
def delete(dbname,tablename,map=None):
	api = domain + '/api/delete'
	request_data = {'dbname':dbname,'tablename':tablename}
	if map != None:
		request_data['map'] = map;
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#更新数据
def update(dbname,tablename,set,map=None):
	api = domain + '/api/update'
	request_data = {'dbname':dbname,'tablename':tablename,'set':set}
	if map != None:
		request_data['map'] = map;
	data = {'session':session,'data':json.dumps(request_data)}

	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#查询数据
def select(dbname,tablename,map=None,columns=None,group=None,order=None,limit=None):
	api = domain + '/api/select'
	request_data = {'dbname':dbname,'tablename':tablename}
	if map != None:
		request_data['map'] = map;
	if columns != None:
		request_data['columns'] = columns;
	if group != None:
		request_data['group'] = group;
	if order != None:
		request_data['order'] = order;
	if limit != None:
		request_data['limit'] = limit;

	data = {'session':session,'data':json.dumps(request_data)}
	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#查询数据量
def count(dbname,tablename,map=None):
	api = domain + '/api/count'
	request_data = {'dbname':dbname,'tablename':tablename}
	if map != None:
		request_data['map'] = map;

	data = {'session':session,'data':json.dumps(request_data)}
	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#表字段
def columns(dbname,tablename):
	api = domain + '/api/columns'
	request_data = {'dbname':dbname,'tablename':tablename}

	data = {'session':session,'data':json.dumps(request_data)}
	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

#执行SQL
def query(dbname,sql):
	api = domain + '/api/query'
	request_data = {'dbname':dbname,'sql':sql}

	data = {'session':session,'data':json.dumps(request_data)}
	postdata = MultipartEncoder(data)
	html = requests.post(api, data=postdata, headers={'Content-Type': postdata.content_type}, verify=False)
	if html.status_code != requests.codes.ok:
		html.raise_for_status()
	return html.text

'''
create_table('test','table1',[
    {"name":"id",    "type":"int primary"},
    {"name":"user",  "type":"text"},
    {"name":"pwd",   "type":"text"},
    {"name":"level", "type":"int"},
    {"name":"money", "type":"double"}
])

create_table('test','table2',[
    {"name":"id",    "type":"int primary"},
    {"name":"user",  "type":"text"},
    {"name":"pwd",   "type":"text"},
    {"name":"level", "type":"int"},
    {"name":"money", "type":"double"}
])

create_table('test','table3',[
    {"name":"id",    "type":"int primary"},
    {"name":"user",  "type":"text"},
    {"name":"pwd",   "type":"text"},
    {"name":"level", "type":"int"},
    {"name":"money", "type":"double"}
])
'''

